<template>
  <div class="app-container">
    <el-row :gutter="20">
      <splitpanes :horizontal="appStore.device === 'mobile'" class="default-theme">
        <pane size="84">
          <el-col>
            <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
              <el-form-item label="选择仓库" prop="name">
                <el-select v-model="queryParams.cangKuId" style="width: 240px" @change="onCangKuSelected">
                  <el-option v-for="item in cangKuList" :label="item.name" :value="item.id" />
                </el-select>
              </el-form-item>
              <el-form-item label="选择货品" prop="name">
                <el-select v-model="queryParams.huoPinId" style="width: 240px">
                  <el-option v-for="item in huoPinList" :label="item.name" :value="item.id" />
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              </el-form-item>
            </el-form>

            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button type="primary" plain icon="Plus" @click="openAdd" >新增</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-upload action="/api/system/rukudan/importExcel" :show-file-list="false"
                           @on-success="importExcelSuccess"
                           @on-error="importExcelError">
                  <el-button type="primary">导入</el-button>
                </el-upload>
              </el-col>
              <el-col :span="1.5">
                <el-button type="primary" icon="Grid" @click="handleExport">导出</el-button>
              </el-col>
            </el-row>

            <el-table v-loading="loading" :data="dataList">
              <el-table-column type="selection" width="50" align="center" />
              <el-table-column label="货品名称" align="center" prop="huoPinName"  />
              <el-table-column label="档口" align="center" prop="dangKouName"  />
              <el-table-column label="所属订单号" align="center" prop="dingDanId"/>
              <el-table-column label="订单详情号" align="center" prop="dingDanDetailId"/>
              <el-table-column label="入库数量" align="center" prop="ruKuCount" />
              <el-table-column label="入库价格" align="center" prop="ruKuPrice" />
              <el-table-column label="单位" align="center" prop="danWei" />
              <el-table-column label="品牌" align="center" prop="pinPai" />
              <el-table-column label="入库时间" align="center" prop="ruKuTime" >
                <template #default="scope">
                  <span>{{ parseTime(scope.row.createTime) }}</span>
                </template>
              </el-table-column>
              <el-table-column label="供应商" align="center" prop="gongYingShangName" />

              <el-table-column label="状态" align="center"  >
                <template #default="scope">
                  <span>{{ STATUS_MAP[scope.row.status] }}</span>
                </template>
              </el-table-column>

              <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
                <template #default="scope">
                  <span v-if="scope.row.status === 0">
                    <el-button link type="primary" icon="Edit" @click="openUpdate(scope.row.id)" >编辑</el-button>
                    <el-button link type="primary" icon="Edit" @click="openAudit(scope.row)" >审核</el-button>
                  </span>
                </template>
              </el-table-column>

            </el-table>
            <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
          </el-col>
        </pane>
      </splitpanes>
    </el-row>

    <el-dialog :title="title" v-model="showEditDialog" width="600px" append-to-body>
      <el-form :model="editForm" label-width="100px">

        <el-row>
          <el-col :span="12">
            <el-form-item label="货品名称" >
              <el-text > {{editForm.huoPinName}} </el-text>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="入库时间" >
              <el-date-picker v-model="editForm.ruKuTime" type="datetime" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="入库数量" >
              <el-input-number v-model="editForm.ruKuCount"  />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="入库价格" >
              <el-input-number v-model="editForm.ruKuPrice"  />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="生产日期" >
              <el-input v-model="editForm.shengChanRiQi"  />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="保质期" >
              <el-input v-model="editForm.baoZhiQi"  />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="生产商" >
              <el-input v-model="editForm.shengChanShang"  />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="showEditDialog = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>


    <!-- 添加或修改用户配置对话框 -->
    <el-dialog title="审核" v-model="showAuditDialog" width="600px" append-to-body>
      <el-form>
        <el-form-item label="供应商">
          {{auditInfo.gongYingShangName}}
        </el-form-item>
        <el-form-item label="档口">
          {{auditInfo.dangKouName}}
        </el-form-item>
        <el-form-item label="货品">
          {{auditInfo.huoPinName}}
        </el-form-item>
        <el-form-item label="数量">
          {{auditInfo.ruKuCount}} {{auditInfo.danWei}}
        </el-form-item>
        <el-form-item label="价格">
          {{auditInfo.ruKuPrice}} 元
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitAudit(1, 0)">入库</el-button>
          <el-button type="primary" @click="submitAudit(1, 1)">直入直出</el-button>
          <el-button type="warning" @click="submitAudit(2, 0)">驳回</el-button>
          <el-button @click="showAuditDialog = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script setup name="RuKuDan">
import useAppStore from '@/store/modules/app'
import {listWithPage, add, getDetail, update, audit, exportListUrl, queryAuditInfo } from "@/api/system/rukudan.js";
import {listUserAll as loadAllCangKu} from "@/api/system/cangku.js";
import { Splitpanes, Pane } from "splitpanes"
import "splitpanes/dist/splitpanes.css"
import {STATUS_MAP} from "@/utils/dict.js";
import {http} from "@/api/system/request.js";

const router = useRouter();
const appStore = useAppStore()
const { proxy } = getCurrentInstance();

const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  name: undefined,
});
const total = ref(0);
const dataList = ref([]);

const editForm = ref({});
const showEditDialog = ref(false);
const loading = ref(true);

const title = ref("");

const editHuoPinList = ref([]);

const cangKuList = ref([]);

/** 查询列表 */
async function getList() {
  loading.value = true;
  let res = await listWithPage(queryParams.value);

  loading.value = false;
  dataList.value = res.rows;
  total.value = res.total;
}

async function getAllCangKu(){
  loading.value = true;

  let res = await loadAllCangKu();
  loading.value = false;
  cangKuList.value = res.rows;
}

onMounted(()=>{
  getList();
  getAllCangKu();
});


/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 新增按钮操作 */
function openAdd() {
  editForm.value = {};
  showEditDialog.value = true;
  title.value = "新建";
}

function importExcelSuccess(){
  proxy.$modal.msgSuccess("导入成功");
  getList();
}

function importExcelError(){
  proxy.$modal.msgWarning("导入失败");
}

function handleExport(){
  proxy.download(exportListUrl, {
    ...queryParams.value,
  },`入库单信息.xlsx`);
}

/** 修改按钮操作 */
async function openUpdate(id) {
  let response = await getDetail(id);

  editForm.value = response.data;
  showEditDialog.value = true;
  title.value = "修改";
}

const auditId = ref(0);
const auditInfo = ref({});
const showAuditDialog = ref(false);

/** 修改按钮操作 */
function openAudit(row) {
  auditId.value = row.id;
  queryAuditInfo(row.id).then(res => {
    auditInfo.value = res.data;
  });
  showAuditDialog.value = true;
}

/**
 * status 审核状态，1通过，2驳回
 * zhiChu 直入直出标志，0仅入库，1直入直出
 * **/
async function submitAudit(status, zhiChu){
  let rsp = await audit(auditId.value, status, zhiChu);
  if(rsp.code === 200){
    showAuditDialog.value = false;
    proxy.$modal.msgSuccess("审核成功");
    getList();
  } else {
    showAuditDialog.value = false;
    proxy.$modal.msgWarning("审核失败");
  }
}

/** 提交按钮 */
function submitForm() {
    if (editForm.value.id != undefined) {
      update(editForm.value).then(response => {
        proxy.$modal.msgSuccess("修改成功");
        showEditDialog.value = false;
        getList();
      });
    } else {
      add(editForm.value).then(response => {
        proxy.$modal.msgSuccess("新增成功");
        showEditDialog.value = false;
        getList();
      });
    }
}



</script>
